package com.doubao.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.doubao.common.result.Result;
import com.doubao.user.dto.response.UserInfoResponse;
import com.doubao.user.dto.response.UserNotificationResponse;
import com.doubao.user.service.UserNotificationService;
import com.doubao.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户通知控制器
 */
@RestController
@RequestMapping("/notification")
@RequiredArgsConstructor
@Tag(name = "用户通知管理", description = "用户通知相关接口")
public class UserNotificationController {

    private final UserService userService;
    private final UserNotificationService userNotificationService;

    /**
     * 获取用户通知列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取用户通知列表", description = "分页获取当前登录用户的通知列表")
    public Result<IPage<UserNotificationResponse>> getUserNotifications(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int size) {
        UserInfoResponse userInfo = userService.getCurrentUserInfo();
        IPage<UserNotificationResponse> notifications = userNotificationService.getUserNotifications(userInfo.getId(), page, size);
        return Result.success(notifications);
    }

    /**
     * 获取用户未读通知数量
     */
    @GetMapping("/unread/count")
    @Operation(summary = "获取用户未读通知数量", description = "获取当前登录用户的未读通知数量")
    public Result<Integer> getUnreadCount() {
        UserInfoResponse userInfo = userService.getCurrentUserInfo();
        int count = userNotificationService.getUnreadCount(userInfo.getId());
        return Result.success(count);
    }

    /**
     * 获取用户最新通知
     */
    @GetMapping("/latest")
    @Operation(summary = "获取用户最新通知", description = "获取当前登录用户的最新几条通知")
    public Result<List<UserNotificationResponse>> getLatestNotifications(
            @RequestParam(value = "limit", defaultValue = "5") int limit) {
        UserInfoResponse userInfo = userService.getCurrentUserInfo();
        List<UserNotificationResponse> notifications = userNotificationService.getLatestNotifications(userInfo.getId(), limit);
        return Result.success(notifications);
    }

    /**
     * 将通知标记为已读
     */
    @PutMapping("/read/{id}")
    @Operation(summary = "将通知标记为已读", description = "将指定通知标记为已读状态")
    public Result<Void> markAsRead(@PathVariable("id") Long notificationId) {
        return userNotificationService.markAsRead(notificationId);
    }

    /**
     * 将所有通知标记为已读
     */
    @PutMapping("/read/all")
    @Operation(summary = "将所有通知标记为已读", description = "将当前登录用户的所有通知标记为已读状态")
    public Result<Void> markAllAsRead() {
        UserInfoResponse userInfo = userService.getCurrentUserInfo();
        return userNotificationService.markAllAsRead(userInfo.getId());
    }

    /**
     * 将指定类型的通知标记为已读
     */
    @PutMapping("/read/type/{type}")
    @Operation(summary = "将指定类型的通知标记为已读", description = "将当前登录用户的指定类型通知标记为已读状态")
    public Result<Void> markTypeAsRead(@PathVariable("type") String type) {
        UserInfoResponse userInfo = userService.getCurrentUserInfo();
        return userNotificationService.markTypeAsRead(userInfo.getId(), type);
    }

    /**
     * 删除通知
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除通知", description = "删除指定的通知")
    public Result<Void> deleteNotification(@PathVariable("id") Long notificationId) {
        return userNotificationService.deleteNotification(notificationId);
    }

    /**
     * 清空所有通知
     */
    @DeleteMapping("/clear")
    @Operation(summary = "清空所有通知", description = "清空当前登录用户的所有通知")
    public Result<Void> clearAllNotifications() {
        UserInfoResponse userInfo = userService.getCurrentUserInfo();
        return userNotificationService.clearAllNotifications(userInfo.getId());
    }

    /**
     * 获取所有通知状态概览
     */
    @GetMapping("/overview")
    @Operation(summary = "获取通知状态概览", description = "获取当前登录用户的通知状态概览信息")
    public Result<Map<String, Object>> getNotificationOverview() {
        UserInfoResponse userInfo = userService.getCurrentUserInfo();
        Long userId = userInfo.getId();

        int unreadCount = userNotificationService.getUnreadCount(userId);
        List<UserNotificationResponse> latestNotifications = userNotificationService.getLatestNotifications(userId, 3);

        Map<String, Object> overview = Map.of(
                "unreadCount", unreadCount,
                "hasUnread", unreadCount > 0,
                "latestNotifications", latestNotifications
        );

        return Result.success(overview);
    }
}